﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gmis.IDAL;
using System.Data;
using System.Data.SqlClient;
using Gmis.DBUtility;

namespace Gmis.SqlServerDAL
{
    /// <summary>
    /// 数据访问类:LogUser
    /// </summary>
    public partial class LogUser : ILogUser
    {
        public LogUser()
        { }
        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Gmis.Model.LogUser model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into LogUser(");
            strSql.Append("sID,sLoginName,sName,sPassWord,sRole,lSex,lSysFlag,lEnable,nIndex,sComment,dCreateTime)");
            strSql.Append(" values (");
            strSql.Append("@sID,@sLoginName,@sName,@sPassWord,@sRole,@lSex,@lSysFlag,@lEnable,@nIndex,@sComment,@dCreateTime)");
            SqlParameter[] parameters = {
					new SqlParameter("@sID", SqlDbType.VarChar,50),
					new SqlParameter("@sLoginName", SqlDbType.VarChar,50),
					new SqlParameter("@sName", SqlDbType.VarChar,50),
					new SqlParameter("@sPassWord", SqlDbType.VarChar,50),
					new SqlParameter("@sRole", SqlDbType.VarChar,50),
					new SqlParameter("@lSex", SqlDbType.Bit,1),
					new SqlParameter("@lSysFlag", SqlDbType.Bit,1),
					new SqlParameter("@lEnable", SqlDbType.Bit,1),
					new SqlParameter("@nIndex", SqlDbType.Int,4),
					new SqlParameter("@sComment", SqlDbType.VarChar,255),
					new SqlParameter("@dCreateTime", SqlDbType.DateTime)};
            parameters[0].Value = model.sID;
            parameters[1].Value = model.sLoginName;
            parameters[2].Value = model.sName;
            parameters[3].Value = model.sPassWord;
            parameters[4].Value = model.sRole;
            parameters[5].Value = model.lSex;
            parameters[6].Value = model.lSysFlag;
            parameters[7].Value = model.lEnable;
            parameters[8].Value = model.nIndex;
            parameters[9].Value = model.sComment;
            parameters[10].Value = model.dCreateTime;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gmis.Model.LogUser model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update LogUser set ");
            strSql.Append("sLoginName=@sLoginName,");
            strSql.Append("sName=@sName,");
            strSql.Append("sPassWord=@sPassWord,");
            strSql.Append("sRole=@sRole,");
            strSql.Append("lSex=@lSex,");
            strSql.Append("lSysFlag=@lSysFlag,");
            strSql.Append("lEnable=@lEnable,");
            strSql.Append("nIndex=@nIndex,");
            strSql.Append("sComment=@sComment,");
            strSql.Append("dCreateTime=@dCreateTime");
            strSql.Append(" where sID=@sID ");
            SqlParameter[] parameters = {
					new SqlParameter("@sLoginName", SqlDbType.VarChar,50),
					new SqlParameter("@sName", SqlDbType.VarChar,50),
					new SqlParameter("@sPassWord", SqlDbType.VarChar,50),
					new SqlParameter("@sRole", SqlDbType.VarChar,50),
					new SqlParameter("@lSex", SqlDbType.Bit,1),
					new SqlParameter("@lSysFlag", SqlDbType.Bit,1),
					new SqlParameter("@lEnable", SqlDbType.Bit,1),
					new SqlParameter("@nIndex", SqlDbType.Int,4),
					new SqlParameter("@sComment", SqlDbType.VarChar,255),
					new SqlParameter("@dCreateTime", SqlDbType.DateTime),
					new SqlParameter("@sID", SqlDbType.VarChar,50)};
            parameters[0].Value = model.sLoginName;
            parameters[1].Value = model.sName;
            parameters[2].Value = model.sPassWord;
            parameters[3].Value = model.sRole;
            parameters[4].Value = model.lSex;
            parameters[5].Value = model.lSysFlag;
            parameters[6].Value = model.lEnable;
            parameters[7].Value = model.nIndex;
            parameters[8].Value = model.sComment;
            parameters[9].Value = model.dCreateTime;
            parameters[10].Value = model.sID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string sID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from LogUser ");
            strSql.Append(" where sID=@sID ");
            SqlParameter[] parameters = {
					new SqlParameter("@sID", SqlDbType.VarChar,50)};
            parameters[0].Value = sID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string sIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from LogUser ");
            strSql.Append(" where sID in (" + sIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gmis.Model.LogUser GetModel(string sID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 sID,sLoginName,sName,sPassWord,sRole,lSex,lSysFlag,lEnable,nIndex,sComment,dCreateTime from LogUser ");
            strSql.Append(" where sID=@sID ");
            SqlParameter[] parameters = {
					new SqlParameter("@sID", SqlDbType.VarChar,50)};
            parameters[0].Value = sID;

            Gmis.Model.LogUser model = new Gmis.Model.LogUser();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["sID"] != null && ds.Tables[0].Rows[0]["sID"].ToString() != "")
                {
                    model.sID = ds.Tables[0].Rows[0]["sID"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sLoginName"] != null && ds.Tables[0].Rows[0]["sLoginName"].ToString() != "")
                {
                    model.sLoginName = ds.Tables[0].Rows[0]["sLoginName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sName"] != null && ds.Tables[0].Rows[0]["sName"].ToString() != "")
                {
                    model.sName = ds.Tables[0].Rows[0]["sName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sPassWord"] != null && ds.Tables[0].Rows[0]["sPassWord"].ToString() != "")
                {
                    model.sPassWord = ds.Tables[0].Rows[0]["sPassWord"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sRole"] != null && ds.Tables[0].Rows[0]["sRole"].ToString() != "")
                {
                    model.sRole = ds.Tables[0].Rows[0]["sRole"].ToString();
                }
                if (ds.Tables[0].Rows[0]["lSex"] != null && ds.Tables[0].Rows[0]["lSex"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["lSex"].ToString() == "1") || (ds.Tables[0].Rows[0]["lSex"].ToString().ToLower() == "true"))
                    {
                        model.lSex = true;
                    }
                    else
                    {
                        model.lSex = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["lSysFlag"] != null && ds.Tables[0].Rows[0]["lSysFlag"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["lSysFlag"].ToString() == "1") || (ds.Tables[0].Rows[0]["lSysFlag"].ToString().ToLower() == "true"))
                    {
                        model.lSysFlag = true;
                    }
                    else
                    {
                        model.lSysFlag = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["lEnable"] != null && ds.Tables[0].Rows[0]["lEnable"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["lEnable"].ToString() == "1") || (ds.Tables[0].Rows[0]["lEnable"].ToString().ToLower() == "true"))
                    {
                        model.lEnable = true;
                    }
                    else
                    {
                        model.lEnable = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["nIndex"] != null && ds.Tables[0].Rows[0]["nIndex"].ToString() != "")
                {
                    model.nIndex = int.Parse(ds.Tables[0].Rows[0]["nIndex"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sComment"] != null && ds.Tables[0].Rows[0]["sComment"].ToString() != "")
                {
                    model.sComment = ds.Tables[0].Rows[0]["sComment"].ToString();
                }
                if (ds.Tables[0].Rows[0]["dCreateTime"] != null && ds.Tables[0].Rows[0]["dCreateTime"].ToString() != "")
                {
                    model.dCreateTime = DateTime.Parse(ds.Tables[0].Rows[0]["dCreateTime"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select sID,sLoginName,sName,sPassWord,sRole,lSex,lSysFlag,lEnable,nIndex,sComment,dCreateTime ");
            strSql.Append(" FROM LogUser ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" sID,sLoginName,sName,sPassWord,sRole,lSex,lSysFlag,lEnable,nIndex,sComment,dCreateTime ");
            strSql.Append(" FROM LogUser ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "LogUser";
            parameters[1].Value = "sID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}
